# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=libgeotiff
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.7.1
pkgrel=4
pkgdesc="TIFF based interchange format for georeferenced raster imagery (mingw-w64)"
arch=(any)
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://github.com/OSGeo/libgeotiff"
license=('MIT')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-libjpeg"
         "${MINGW_PACKAGE_PREFIX}-proj"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("https://github.com/OSGeo/${_realname}/releases/download/$pkgver/${_realname}-$pkgver.tar.gz"
        'libgeotiff_buildsys.patch')
sha256sums=('05ab1347aaa471fc97347d8d4269ff0c00f30fa666d956baba37948ec87e55d6'
            'bfc377673bbc0e36cebda77f6bdff68f69ac591054843d4050a2da94bb879e76')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i ${srcdir}/libgeotiff_buildsys.patch

  # fix wrongly encoded files from tarball
  for f in $(find . -type f); do
    if file $f | grep -q ISO-8859 ; then
      /bin/iconv -f ISO-8859-1 -t UTF-8 $f > ${f}.tmp && \
        mv -f ${f}.tmp $f
    fi
    if file $f | grep -q CRLF ; then
      sed -i -e 's|\r$||g' $f
    fi
  done
}

build() {

  declare -a _build_type
  if check_option "debug" "n"; then
    _build_type+=("Release")
  else
    _build_type+=("Debug")
  fi

  # Static
  mkdir "${srcdir}/build-${MSYSTEM}-static" && cd "${srcdir}/build-${MSYSTEM}-static"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=${_build_type} \
    -DBUILD_SHARED_LIBS=OFF \
    -DWITH_TIFF=ON \
    -DWITH_JPEG=ON \
    -DWITH_ZLIB=ON \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .

  # Shared
  mkdir "${srcdir}/build-${MSYSTEM}-shared" && cd "${srcdir}/build-${MSYSTEM}-shared"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=${_build_type} \
    -DBUILD_SHARED_LIBS=ON \
    -DWITH_TIFF=ON \
    -DWITH_JPEG=ON \
    -DWITH_ZLIB=ON \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install "${srcdir}/build-${MSYSTEM}-static"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install "${srcdir}/build-${MSYSTEM}-shared"

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/share/cmake/GeoTIFF/*.cmake; do
    sed -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
    sed -e "s|${MINGW_PREFIX}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
  done

  mkdir -p ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/

  cat > ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/${_realname}.pc <<EOF
prefix=${MINGW_PREFIX}
exec_prefix=\${prefix}
libdir=\${prefix}/lib
includedir=\${prefix}/include

Name: ${_realname}
Description: GeoTIFF file format library
Version: ${pkgver}
Libs: -L\${libdir} -lgeotiff
Cflags: -I\${includedir}
EOF

}
